﻿using System;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DTU_Test
{
    public partial class Form1 : Form
    {
        /// <summary>
        /// 通信串口
        /// </summary>
        private SerialPort serialPort1 = new SerialPort();

        public Form1()
        {
            InitializeComponent();

            var d = new byte[] { 0x85, 0x0a };
            uint value = BitConverter.ToUInt16(d, 0);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.comboBox1.Items.AddRange(System.IO.Ports.SerialPort.GetPortNames());
            this.comboBox3.SelectedIndex = 1;
            this.comboBox2.SelectedIndex = 0; // 默认加密方式
            this.textBox1.Text = "2025051800000001"; //$"{DateTime.Now.ToString("yyyyMMdd")}00000001"; // 设备名称
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DTUSetting setting = new DTUSetting();
            setting.DeviceName = this.textBox1.Text.Trim();
            setting.ServerIP = this.textBox2.Text.Trim();
            setting.ServerPort = int.Parse(this.textBox7.Text); // 默认端口号
            setting.HeartBeatInterval = 30; // 默认心跳间隔时间
            setting.APNName = textBox3.Text; // 默认APN名称
            setting.UserName = textBox4.Text; // 默认APN用户名
            setting.Password = textBox5.Text; // 默认APN密码
            setting.Encryption = comboBox2.SelectedIndex; // 默认加密方式
            setting.BaudRate = int.Parse(comboBox3.SelectedItem.ToString()); // 默认波特率
            setting.DataBits = 8; // 默认数据位
            setting.StopBits = 1; // 默认停止位
            setting.Parity = 0; // 默认无校验

            try
            {
                serialPort1.PortName = this.comboBox1.Text;
                serialPort1.BaudRate = 9600;
                serialPort1.DataBits = 8;
                serialPort1.StopBits = StopBits.One;
                serialPort1.Parity = Parity.None;
                if (!serialPort1.IsOpen)
                {
                    serialPort1.Open();// 打开串口

                    var frame = DTU_Protocol_Frame.BuildWriteDeviceInfoFrame(setting); // 构建写入设备信息的协议帧
                    byte[] data = frame.ToBytes(); // 将协议帧转换为字节数组
                    this.serialPort1.Write(data, 0, data.Length); // 发送数据

                    //停顿一下，读取返回数据
                    System.Threading.Thread.Sleep(2000);

                    if (serialPort1.BytesToRead > 0)
                    {
                        byte[] buffer = new byte[serialPort1.BytesToRead];
                        serialPort1.Read(buffer, 0, buffer.Length);
                        string response = buffer.ToHexString(); // 将字节数组转换成十六进制字符串
                        MessageBox.Show("返回数据：" + response);
                    }
                    else
                    {
                        MessageBox.Show("没有返回数据！");
                    }

                    serialPort1.Close(); // 关闭串口
                }
                else
                {
                    MessageBox.Show("串口已打开，请先关闭串口！");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("打开串口失败：" + ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                this.textBox1.Text = string.Empty; // 清空文本框
                this.textBox2.Text = string.Empty; // 清空文本框
                this.textBox3.Text = string.Empty; // 清空文本框
                this.textBox4.Text = string.Empty; // 清空文本框
                this.textBox5.Text = string.Empty; // 清空文本框
                this.textBox7.Text = string.Empty; // 默认端口号
                this.Update();
                serialPort1.PortName = this.comboBox1.Text;
                serialPort1.BaudRate = 9600;
                serialPort1.DataBits = 8;
                serialPort1.StopBits = StopBits.One;
                serialPort1.Parity = Parity.None;
                if (!serialPort1.IsOpen)
                {
                    serialPort1.Open();// 打开串口

                    var frame = DTU_Protocol_Frame.BuildReadDeviceInfoFrame(); // 构建写入设备信息的协议帧
                    byte[] data = frame.ToBytes(); // 将协议帧转换为字节数组
                    this.serialPort1.Write(data, 0, data.Length); // 发送数据

                    //停顿一下，读取返回数据
                    System.Threading.Thread.Sleep(2000);

                    if (serialPort1.BytesToRead > 0)
                    {
                        byte[] buffer = new byte[serialPort1.BytesToRead];
                        serialPort1.Read(buffer, 0, buffer.Length);

                        //把数组转换成字符串
                        string response = ASCIIEncoding.Default.GetString(buffer);
                        DTUSetting setting = DTU_Protocol_Frame.DecodeDTUSetting(buffer); // 解析返回的数据
                        if (setting != null)
                        {
                            switch (setting.BaudRate)
                            {
                                case 4800:
                                    {
                                        comboBox3.SelectedIndex = 0;
                                    }
                                    break;

                                case 9600:
                                    {
                                        comboBox3.SelectedIndex = 1;
                                    }
                                    break;

                                case 19200:
                                    {
                                        comboBox3.SelectedIndex = 2;
                                    }
                                    break;

                                case 38400:
                                    {
                                        comboBox3.SelectedIndex = 3;
                                    }
                                    break;

                                case 57600:
                                    {
                                        comboBox3.SelectedIndex = 4;
                                    }
                                    break;

                                case 115200:
                                    {
                                        comboBox3.SelectedIndex = 5;
                                    }
                                    break;
                            }
                            comboBox2.SelectedIndex = setting.Encryption; // 默认加密方式
                            this.textBox1.Text = setting?.DeviceName;
                            this.textBox2.Text = setting?.ServerIP;
                            this.textBox3.Text = setting?.APNName; // 默认APN名称
                            this.textBox4.Text = setting?.UserName; // 默认APN用户名
                            this.textBox5.Text = setting?.Password; // 默认APN密码
                            this.textBox7.Text = setting?.ServerPort.ToString(); // 默认端口号
                        }

                        // string response = buffer.ToHexString(); // 将字节数组转换成十六进制字符串
                        MessageBox.Show("返回数据：" + response);
                    }
                    else
                    {
                        MessageBox.Show("没有返回数据！");
                    }

                    serialPort1.Close(); // 关闭串口
                }
                else
                {
                    MessageBox.Show("串口已打开，请先关闭串口！");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("打开串口失败：" + ex.Message);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                serialPort1.PortName = this.comboBox1.Text;
                serialPort1.BaudRate = 9600;
                serialPort1.DataBits = 8;
                serialPort1.StopBits = StopBits.One;
                serialPort1.Parity = Parity.None;
                if (!serialPort1.IsOpen)
                {
                    serialPort1.Open();// 打开串口

                    byte[] data = new byte[] { 0x01, 0x03, 0x00, 0x01, 0x00, 0x01, 0xd5, 0xca }; // 将协议帧转换为字节数组
                    this.serialPort1.Write(data, 0, data.Length); // 发送数据

                    //停顿一下，读取返回数据
                    System.Threading.Thread.Sleep(1000);

                    if (serialPort1.BytesToRead > 0)
                    {
                        byte[] buffer = new byte[serialPort1.BytesToRead];
                        serialPort1.Read(buffer, 0, buffer.Length);
                        //MessageBox.Show("返回数据：" + buffer.ToHexString()); // 将字节数组转换成十六进制字符串
                        if (buffer.Length == 7)
                        {
                            uint value = BitConverter.ToUInt16(new byte[] { buffer[4], buffer[3] }, 0);
                            this.label8.Text = "液位值：" + value / 1000f;
                        }
                        else
                        {
                            MessageBox.Show("返回数据格式不正确！");
                        }
                    }
                    else
                    {
                        MessageBox.Show("没有返回数据！");
                    }

                    serialPort1.Close(); // 关闭串口
                }
                else
                {
                    MessageBox.Show("串口已打开，请先关闭串口！");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("打开串口失败：" + ex.Message);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                //读取设备模板

                string template = System.IO.File.ReadAllText(".\\mppt_modbus.json");
                var dataPoint = Helper.DeSerializeObject<ModbusDataPoint[]>(template);
                if (dataPoint != null && dataPoint.Length > 0)
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (var item in dataPoint)
                    {
                        // sb.AppendLine($"名称: {item.Name}, 站号: {item.SlaveId}, 功能码: {item.FunctionCode}, 地址: {item.Address}, 个数: {item.Count}, 值: {item.Value}, 类型: {item.ValueType}, 大小端: {item.EndianType}, 比率: {item.Rate}");
                        if (item.FunctionCode == 03)
                        {
                            sb.AppendLine("待发送字节数组:" + item.ToBytes().ToHexString());
                        }
                    }
                    this.textBox6.Text = sb.ToString();
                }
                else
                {
                    MessageBox.Show("没有读取到数据点信息！");
                    return;
                }

                serialPort1.PortName = this.comboBox1.Text;
                serialPort1.BaudRate = 9600;
                serialPort1.DataBits = 8;
                serialPort1.StopBits = StopBits.One;
                serialPort1.Parity = Parity.None;
                if (!serialPort1.IsOpen)
                {
                    serialPort1.Open();// 打开串口

                    StringBuilder sb = new StringBuilder();
                    foreach (var item in dataPoint)
                    {
                        if (item.FunctionCode == 03)
                        {
                            byte[] data = item.ToBytes(); // 将协议帧转换为字节数组
                            this.serialPort1.Write(data, 0, data.Length); // 发送数据
                            System.Threading.Thread.Sleep(1000);//停顿一下，读取返回数据
                            if (serialPort1.BytesToRead > 0)
                            {
                                byte[] buffer = new byte[serialPort1.BytesToRead];
                                serialPort1.Read(buffer, 0, buffer.Length);
                                if (item.ParseData(buffer)) // 解析Modbus数据点的值
                                {
                                    sb.AppendLine($"名称: {item.Name}, 值: {item.Value}");
                                }
                                else
                                {
                                    sb.AppendLine($"名称: {item.Name}, 值解析失败");
                                }
                            }
                            else
                            {
                                sb.AppendLine($"名称: {item.Name}, 没有返回数据！");
                            }
                        }
                    }
                    this.textBox6.Text += sb.ToString(); // 显示所有数据点的值

                    serialPort1.Close(); // 关闭串口
                }
                else
                {
                    MessageBox.Show("串口已打开，请先关闭串口！");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("打开串口失败：" + ex.Message);
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.textBox6.Text = string.Empty; // 清空文本框
        }

        private void button6_Click(object sender, EventArgs e)
        {
            try
            {
                //读取设备模板

                string template = System.IO.File.ReadAllText(".\\mppt_modbus.json");
                var dataPoint = Helper.DeSerializeObject<ModbusDataPoint[]>(template);

                var item = dataPoint.FirstOrDefault(x => x.Name == "TurnOn" && x.FunctionCode == 06);

                var bytes = item.ToBytes();

                if (item == null)
                {
                    MessageBox.Show("没有读取到开机数据点信息！");
                    return;
                }

                serialPort1.PortName = this.comboBox1.Text;
                serialPort1.BaudRate = 9600;
                serialPort1.DataBits = 8;
                serialPort1.StopBits = StopBits.One;
                serialPort1.Parity = Parity.None;
                if (!serialPort1.IsOpen)
                {
                    serialPort1.Open();// 打开串口

                    StringBuilder sb = new StringBuilder();

                    byte[] data = item.ToBytes(); // 将协议帧转换为字节数组
                    this.serialPort1.Write(data, 0, data.Length); // 发送数据
                    System.Threading.Thread.Sleep(1000);//停顿一下，读取返回数据
                    if (serialPort1.BytesToRead > 0)
                    {
                        byte[] buffer = new byte[serialPort1.BytesToRead];
                        serialPort1.Read(buffer, 0, buffer.Length);
                        if (item.ParseData(buffer)) // 解析Modbus数据点的值
                        {
                            sb.AppendLine($"名称: {item.Name}, 值: {item.Value}");
                        }
                        else
                        {
                            sb.AppendLine($"名称: {item.Name}, 值解析失败");
                        }
                    }
                    else
                    {
                        sb.AppendLine($"名称: {item.Name}, 没有返回数据！");
                    }

                    this.textBox6.Text += sb.ToString(); // 显示所有数据点的值

                    serialPort1.Close(); // 关闭串口
                }
                else
                {
                    MessageBox.Show("串口已打开，请先关闭串口！");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("打开串口失败：" + ex.Message);
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            try
            {
                //读取设备模板

                string template = System.IO.File.ReadAllText(".\\mppt_modbus.json");
                var dataPoint = Helper.DeSerializeObject<ModbusDataPoint[]>(template);

                var item = dataPoint.FirstOrDefault(x => x.Name == "TurnOff" && x.FunctionCode == 06);

                if (item == null)
                {
                    MessageBox.Show("没有读取到开机数据点信息！");
                    return;
                }

                serialPort1.PortName = this.comboBox1.Text;
                serialPort1.BaudRate = 9600;
                serialPort1.DataBits = 8;
                serialPort1.StopBits = StopBits.One;
                serialPort1.Parity = Parity.None;
                if (!serialPort1.IsOpen)
                {
                    serialPort1.Open();// 打开串口

                    StringBuilder sb = new StringBuilder();

                    byte[] data = item.ToBytes(); // 将协议帧转换为字节数组
                    this.serialPort1.Write(data, 0, data.Length); // 发送数据
                    System.Threading.Thread.Sleep(1000);//停顿一下，读取返回数据
                    if (serialPort1.BytesToRead > 0)
                    {
                        byte[] buffer = new byte[serialPort1.BytesToRead];
                        serialPort1.Read(buffer, 0, buffer.Length);
                        if (item.ParseData(buffer)) // 解析Modbus数据点的值
                        {
                            sb.AppendLine($"名称: {item.Name}, 值: {item.Value}");
                        }
                        else
                        {
                            sb.AppendLine($"名称: {item.Name}, 值解析失败");
                        }
                    }
                    else
                    {
                        sb.AppendLine($"名称: {item.Name}, 没有返回数据！");
                    }

                    this.textBox6.Text += sb.ToString(); // 显示所有数据点的值

                    serialPort1.Close(); // 关闭串口
                }
                else
                {
                    MessageBox.Show("串口已打开，请先关闭串口！");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("打开串口失败：" + ex.Message);
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            try
            {
                string template = System.IO.File.ReadAllText(".\\Hygrothermograph.json");
                var dataPoint = Helper.DeSerializeObject<ModbusDataPoint[]>(template);

                if (dataPoint != null)

                {
                    serialPort1.PortName = this.comboBox1.Text;
                    serialPort1.BaudRate = 9600;
                    serialPort1.DataBits = 8;
                    serialPort1.StopBits = StopBits.One;
                    serialPort1.Parity = Parity.None;
                    if (!serialPort1.IsOpen)
                    {
                        serialPort1.Open();// 打开串口
                    }

                    foreach (ModbusDataPoint item in dataPoint)
                    {
                        StringBuilder sb = new StringBuilder();
                        byte[] data = item.ToBytes(); // 将协议帧转换为字节数组
                        this.serialPort1.Write(data, 0, data.Length); // 发送数据
                        System.Threading.Thread.Sleep(1000);//停顿一下，读取返回数据
                        if (serialPort1.BytesToRead > 0)
                        {
                            byte[] buffer = new byte[serialPort1.BytesToRead];
                            serialPort1.Read(buffer, 0, buffer.Length);
                            if (item.ParseData(buffer)) // 解析Modbus数据点的值
                            {
                                sb.AppendLine($"名称: {item.Name}, 值: {item.Value}");
                            }
                            else
                            {
                                sb.AppendLine($"名称: {item.Name}, 值解析失败");
                            }
                        }
                        else
                        {
                            sb.AppendLine($"名称: {item.Name}, 没有返回数据！");
                        }

                        this.textBox6.Text += sb.ToString(); // 显示所有数据点的值
                    }

                    if (serialPort1.IsOpen)
                    {
                        serialPort1.Close();// 打开串口
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("打开串口失败：" + ex.Message);
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            try
            {
                string template = System.IO.File.ReadAllText(".\\DTSU666.json");
                var dataPoint = Helper.DeSerializeObject<ModbusDataPoint[]>(template);

                if (dataPoint != null)

                {
                    serialPort1.PortName = this.comboBox1.Text;
                    serialPort1.BaudRate = 9600;
                    serialPort1.DataBits = 8;
                    serialPort1.StopBits = StopBits.One;
                    serialPort1.Parity = Parity.None;
                    if (!serialPort1.IsOpen)
                    {
                        serialPort1.Open();// 打开串口
                    }

                    foreach (ModbusDataPoint item in dataPoint)
                    {
                        StringBuilder sb = new StringBuilder();
                        byte[] data = item.ToBytes(); // 将协议帧转换为字节数组
                        this.serialPort1.Write(data, 0, data.Length); // 发送数据
                        System.Threading.Thread.Sleep(1000);//停顿一下，读取返回数据
                        if (serialPort1.BytesToRead > 0)
                        {
                            byte[] buffer = new byte[serialPort1.BytesToRead];
                            serialPort1.Read(buffer, 0, buffer.Length);
                            if (item.ParseData(buffer)) // 解析Modbus数据点的值
                            {
                                sb.AppendLine($"名称: {item.Name}, 值: {item.Value}");
                            }
                            else
                            {
                                sb.AppendLine($"名称: {item.Name}, 值解析失败");
                            }
                        }
                        else
                        {
                            sb.AppendLine($"名称: {item.Name}, 没有返回数据！");
                        }

                        this.label13.Text += sb.ToString(); // 显示所有数据点的值
                    }

                    if (serialPort1.IsOpen)
                    {
                        serialPort1.Close();// 打开串口
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("打开串口失败：" + ex.Message);
            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            try
            {
                string template = System.IO.File.ReadAllText(".\\transmiiter.json");
                var dataPoint = Helper.DeSerializeObject<ModbusDataPoint[]>(template);

                if (dataPoint != null)

                {
                    serialPort1.PortName = this.comboBox1.Text;
                    serialPort1.BaudRate = 9600;
                    serialPort1.DataBits = 8;
                    serialPort1.StopBits = StopBits.One;
                    serialPort1.Parity = Parity.None;
                    if (!serialPort1.IsOpen)
                    {
                        serialPort1.Open();// 打开串口
                    }

                    foreach (ModbusDataPoint item in dataPoint)
                    {
                        StringBuilder sb = new StringBuilder();
                        byte[] data = item.ToBytes(); // 将协议帧转换为字节数组
                        this.serialPort1.Write(data, 0, data.Length); // 发送数据
                        System.Threading.Thread.Sleep(1000);//停顿一下，读取返回数据
                        if (serialPort1.BytesToRead > 0)
                        {
                            byte[] buffer = new byte[serialPort1.BytesToRead];
                            serialPort1.Read(buffer, 0, buffer.Length);
                            if (item.ParseData(buffer)) // 解析Modbus数据点的值
                            {
                                sb.AppendLine($"名称: {item.Name}, 值: {item.Value}");
                            }
                            else
                            {
                                sb.AppendLine($"名称: {item.Name}, 值解析失败");
                            }
                            MessageBox.Show(sb.ToString());
                        }
                        else
                        {
                            sb.AppendLine($"名称: {item.Name}, 没有返回数据！");
                        }

                        // this.label13.Text += sb.ToString(); // 显示所有数据点的值
                    }

                    if (serialPort1.IsOpen)
                    {
                        serialPort1.Close();// 打开串口
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("打开串口失败：" + ex.Message);
            }
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox2_SelectedIndexChanged != null)
            {
                if (comboBox2.SelectedIndex == 0)
                {
                    textBox2.Text = "115.29.149.229";
                    textBox3.Text = string.Empty;
                    textBox4.Text = string.Empty;
                    textBox5.Text = string.Empty;
                    label6.ForeColor = System.Drawing.SystemColors.ControlText;
                    label6.Text = "用户名称:";
                    textBox7.Text = "6666";
                }
                else
                {
                    textBox2.Text = "10.10.10.20";
                    textBox3.Text = "public.vpdn";
                    textBox4.Text = "xxxxxxx@wlwhzjwu.vpdn.zj";
                    textBox5.Text = "jw26298";
                    textBox7.Text = "5555";
                    label6.ForeColor = System.Drawing.Color.Red;
                    label6.Text = "用户名称:(替换x)";
                }
            }
        }

        
        private void button11_Click(object sender, EventArgs e)
        {
            byte[] cmd1 = new byte[] { 0x01, 0x03, 00, 00, 00, 01, 0x84, 0x0a }; // 温度读取
            byte[] cmd2 = new byte[] { 0x01, 0x03, 00, 01, 00, 01, 0xd5, 0xca }; // 湿度读取
            byte[] cmd3 = new byte[] { 0xaa };//风速
           
            try
            {
                var client = new ChengshuApiClient();
                client.Login();
                string deviceid = "2025091100000001";
                serialPort1.PortName = this.comboBox1.Text;
                serialPort1.BaudRate = 115200;
                serialPort1.DataBits = 8;
                serialPort1.StopBits = StopBits.One;
                serialPort1.Parity = Parity.None;
                if (!serialPort1.IsOpen)
                    serialPort1.Open();// 打开串口
                                       //读温度
                this.serialPort1.Write(cmd1, 0, cmd1.Length); // 发送数据
                    //停顿一下，读取返回数据
                    System.Threading.Thread.Sleep(2000);
                    if (serialPort1.BytesToRead > 0)
                    {
                        byte[] buffer = new byte[serialPort1.BytesToRead];
                        serialPort1.Read(buffer, 0, buffer.Length);
                        if (buffer.Length == 7)
                        {
                            int value = BitConverter.ToInt16(new byte[] { buffer[4], buffer[3] }, 0);
                            TelemetryData telemetryData = new TelemetryData();
                            telemetryData.ts = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
                            telemetryData.Values.Add("水温", value / 10);
                            client.PushDeviceData(deviceid, telemetryData);
                            client.PushDeviceData(deviceid, true);
                            textBox8.Text += $"温度值：{value / 10}°C \r\n";
                        }
                    }
                    else
                    {
                        MessageBox.Show("没有返回温湿度数据！");
                    }

                    //读湿度
                    this.serialPort1.Write(cmd2, 0, cmd2.Length); // 发送数据
                    //停顿一下，读取返回数据
                    System.Threading.Thread.Sleep(2000);

                    if (serialPort1.BytesToRead > 0)
                    {
                        byte[] buffer = new byte[serialPort1.BytesToRead];
                        serialPort1.Read(buffer, 0, buffer.Length);
                        if (buffer.Length == 7)
                        {
                            int value = BitConverter.ToInt16(new byte[] { buffer[4], buffer[3] }, 0);
                            TelemetryData telemetryData = new TelemetryData();
                            telemetryData.ts = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
                            telemetryData.Values.Add("humidity", value / 10);
                            client.PushDeviceData(deviceid, telemetryData);
                            client.PushDeviceData(deviceid, true);
                            textBox8.Text += $"湿度值：{value / 10}% \r\n";
                        }
                    }
                    else
                    {
                        MessageBox.Show("没有返回温湿度数据！");
                    }

                    //读风速
                    this.serialPort1.Write(cmd3, 0, cmd3.Length); // 发送数据
                    //停顿一下，读取返回数据
                    System.Threading.Thread.Sleep(2000);
                    if (serialPort1.BytesToRead > 0)
                    {
                        byte[] buffer = new byte[serialPort1.BytesToRead];
                        serialPort1.Read(buffer, 0, buffer.Length);
                        if (buffer.Length == 3 && buffer[0] == 0xbb)
                        {
                            TelemetryData telemetryData = new TelemetryData();
                            telemetryData.ts = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
                            telemetryData.Values.Add("windspeed", buffer[2]);
                            client.PushDeviceData(deviceid, telemetryData);
                            client.PushDeviceData(deviceid, true);
                            textBox8.Text += $"风速值：{buffer[2]} km/h \r\n";
                        }
                    }
                    else
                    {
                        MessageBox.Show("没有返回风速数据！");
                    }
                if (serialPort1.IsOpen)
                    serialPort1.Close(); // 关闭串口
            }
                catch (Exception ex)
                {
                    MessageBox.Show("打开串口失败：" + ex.Message);
                }
           
            textBox8.ScrollToCaret();
           

        }


        

        private void button13_Click(object sender, EventArgs e)
        {
            long i = long.Parse(this.textBox1.Text);
            i++;
            this.textBox1.Text = i.ToString();
        }

        private void button14_Click(object sender, EventArgs e)
        {
            long i = long.Parse(this.textBox1.Text);
            i--;
            this.textBox1.Text = i.ToString();
        }
    }
}